<?php

abstract class McCube_Notifier_Abstract
    implements McCube_Notifier_Interface {

    abstract protected function _decoratorCallback($match);

    protected $_tokensForFilter = array(
          'McCube_Notifier'
        //, 'McCube_Error'
        , 'McCube_Exception'
    );

    /** */
    protected function _getBacktraces($backtraces=null){
        if(!is_array($backtraces))
            $backtraces = debug_backtrace();

        $backtraces = array_filter($backtraces, array($this, '_getBacktracesFilter'));
        return $backtraces;
    }

    /** */
    protected function _getLastBacktrace(){
        $backtraces = debug_backtrace();
        $backtraces = array_filter($backtraces, array($this, '_getBacktracesFilter'));
        return current($backtraces);
    }

    /** */
    protected function _getBacktracesFilter($backtrace){
        foreach($this->_tokensForFilter as $token){
            if(isset($backtrace['class']) && strpos($backtrace['class'], $token) > -1) return false;
        }
        return true;
    }

    /** */
    protected function _decorator($text){
        return preg_replace_callback('~\[@(.[^@]*)@(.[^@]*)@]~', array($this, '_decoratorCallback'), $text);
    }

}